package com.tianji.remark.service.impl;

import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.ObjectUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.dto.LikedTimesDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author 老李头儿
 * @since 2025-03-30
 */
//@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {
    private final RabbitMqHelper rabbitMqHelper;


    /**
     * @param recordDTO
     * @Description 点赞或取消点赞
     * @Author 老李头儿
     * @Date 2025年03月30 18:38:32
     * @Return void
     **/

    @Override
    public void addLikeRecord(LikeRecordFormDTO recordDTO) {
        //根据参数判断是点赞还是取消点赞
        Boolean TrueorFalse = recordDTO.getLiked() ? like(recordDTO) : dislike(recordDTO);
        if (TrueorFalse) {
            //如果成功，则统计点赞次数
            Integer likedTimes = lambdaQuery().eq(LikedRecord::getBizId, recordDTO.getBizId())
                    .eq(LikedRecord::getBizType, recordDTO.getBizType())
                    .count();
            //发送MQ消息，通知其他服务
            rabbitMqHelper.send(
                    LIKE_RECORD_EXCHANGE,
                    StringUtils.format(LIKED_TIMES_KEY_TEMPLATE, recordDTO.getBizType()),
                    LikedTimesDTO.of(recordDTO.getBizId(), likedTimes));
        }

    }

    /**
     * @param bizIds
     * @Description 查询指定业务id的点赞状态
     * @Author 老李头儿
     * @Date 2025年03月30 20:55:31
     * @Return java.util.Set<java.lang.Long>
     **/

    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {
        //获取用户
        Long userId = UserContext.getUser();
        // 查询点赞状态
        List<LikedRecord> list = lambdaQuery()
                .in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getUserId, userId)
                .list();
        if (ObjectUtils.isEmpty(list)) {
            return Set.of();
        }
        // 返回点赞状态
        return list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());

    }

    /**
    * @Description 假装写一下
    * @Author 老李头儿
    * @Date 2025年04月01 11:02:09
    * @param bizType
    * @param maxBizSize
    * @Return void
    **/

    @Override
    public void readLikedTimesAndSendMessage(String bizType, int maxBizSize) {

    }


    /**
     * @param recordDTO
     * @Description 是取消操作
     * @Author 老李头儿
     * @Date 2025年03月30 20:46:38
     * @Return java.lang.Boolean
     **/

    private Boolean dislike(LikeRecordFormDTO recordDTO) {
        //直接删除记录
        return lambdaUpdate().eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, recordDTO.getBizId())
                .eq(LikedRecord::getBizType, recordDTO.getBizType())
                .remove();
    }

    /**
     * @param recordDTO
     * @Description 是点赞操作
     * @Author 老李头儿
     * @Date 2025年03月30 20:46:15
     * @Return java.lang.Boolean
     **/

    private Boolean like(LikeRecordFormDTO recordDTO) {
        //判断是否已经点赞
        if (lambdaQuery().eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, recordDTO.getBizId())
                .eq(LikedRecord::getBizType, recordDTO.getBizType())
                .count() > 0) {
            return false;
        }
        //添加点赞记录
        LikedRecord record = new LikedRecord();
        record.setUserId(UserContext.getUser());
        record.setBizId(recordDTO.getBizId());
        record.setBizType(recordDTO.getBizType());
        return save(record);
    }
}
